package com.tfy.liquor.controller;

import com.tfy.liquor.bean.ProductOrder;
import com.tfy.liquor.bean.Result;
import com.tfy.liquor.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {
    @Autowired
    private ProductOrderService productOrderService;

    // 获取一个订单编号
    @RequestMapping("/getOrderId")
    public Result getOrderId() {
        HashMap<String, String> map = new HashMap<>();
        map.put("parentOrderId", productOrderService.getOrderId());
        return new Result(map).ok();
    }

    // 创建订单信息
    @RequestMapping("/createProductOrder")
    public Result createProductOrder(ProductOrder productOrder) {
        if (null == productOrder || null == productOrder.getUserId() || null == productOrder.getProductId() || null == productOrder.getAddressId()) {
            return new Result().bad_CodeIsNull();
        }
        if (productOrderService.createProductOrder(productOrder)) {
            return new Result().ok("创建订单成功！");
        }
        return new Result().error("创建订单失败！");
    }

    // 导出订单信息
    @RequestMapping("/exportProductOrder")
    public void exportProductOrder(HttpServletResponse response, ProductOrder productOrder) {
        if (null == productOrder) {
            return;
        }
        productOrderService.exportProductOrder(response, productOrder);
    }

    // 根据用户编号和父订单编号获取订单付款金额
    @RequestMapping("/getOrderAmountByParentOrderId")
    public Result getOrderAmountByParentOrderId(Long userId, String parentOrderId) {
        if (null == userId || null == parentOrderId || "".equals(parentOrderId.trim())) {
            return new Result().bad_CodeIsNull();
        }
        HashMap<String, String> map=new HashMap<>();
        map.put("actualPayment",productOrderService.getOrderAmountByParentOrderId(userId, parentOrderId));
        return new Result(map).ok();
    }
}
